//
//  SSHandpickViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/12.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSHandpickViewController.h"
#import "SSAdvertCell.h"
#import "SSHandPickViewCell.h"
#import "SSHandpickModel.h"
#import "SSHandpickHeaderView.h"
#import "SSApplicDetailViewController.h"

@interface SSHandpickViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *imageNameArray;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

static NSString *const AdvertCellID = @"SSAdvertCellID";
static NSString *const handpickViewCellID = @"handpickViewCellID";
static NSString *const handpickHeaderViewID = @"handpickHeaderViewID";

@implementation SSHandpickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"精选";
    [self setupCollectionView];
   
   
}

- (void) setupCollectionView {
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestData];
    }];
    
    [self.collectionView.mj_header beginRefreshing];
    
}

- (void)requestData {
    
    [self showHUD];
    NSDictionary *param = @{@"platform":@(3),@"cquantity":@(10)};
    [SSNetworkManager getWithURLString:kSSHandPickListAPI parameters:param success:^(NSDictionary *responseDict) {
       
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.dataSource removeAllObjects];
        }
        
        for (NSDictionary *dict in responseDict[@"data"]) {
            SSHandpickModel *handpickModel = [SSHandpickModel mj_objectWithKeyValues:dict];
            [self.dataSource addObject:handpickModel];
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self dismissHUD];
    } failure:^(NSError *error) {
       
        [self dismissHUD];
        [self.collectionView.mj_header endRefreshing];
    }];
    

}

- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        return self.dataSource.count;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 150);
    }else{
        return CGSizeMake( (kScreenWidth - 40 ) / 2, 120);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SSAdvertCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AdvertCellID forIndexPath:indexPath];
        cell.imageNameArr = self.imageNameArray;
        kWeakSelf(self, weakSelf);
        cell.clickBlock = ^(NSInteger index){
            [weakSelf showTip:[NSString stringWithFormat:@"当前点击了%ld张图片",index]];
        };
        return cell;
    }else{
        SSHandPickViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:handpickViewCellID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
         return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return nil;
    }else{
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            SSHandpickHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:handpickHeaderViewID forIndexPath:indexPath];
            
            return headerView;
        }else{
            return nil;
        }
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 0);
    }else{
        return CGSizeMake(kScreenWidth, 54);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    }else{
        SSApplicDetailViewController *detailViewContrller = [[SSApplicDetailViewController alloc] init];
        detailViewContrller.guid = [self.dataSource[indexPath.row] guid];
        [self.navigationController pushViewController:detailViewContrller animated:YES];
    
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[SSAdvertCell class] forCellWithReuseIdentifier:AdvertCellID];
        [_collectionView registerClass:[SSHandPickViewCell class] forCellWithReuseIdentifier:handpickViewCellID];
        [_collectionView registerClass:[SSHandpickHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:handpickHeaderViewID];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)imageNameArray {
    if (!_imageNameArray) {
        _imageNameArray = [NSMutableArray arrayWithArray:@[@"h6",@"h7"]];
    }
    return _imageNameArray;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
